Allow multiples of fifteen as seconds.

Akinori MUSHA 10 years ago
parent
commit
53067263cf
3 changed files with 20 additions and 13 deletions
  1. 1 1
      .env.example
  2. 9 11
      app/models/agents/scheduler_agent.rb
  3. 10 1
      spec/models/agents/scheduler_agent_spec.rb

+ 1 - 1
.env.example

@@ -121,7 +121,7 @@ ENABLE_INSECURE_AGENTS=false
121 121
 # Enable this setting to allow second precision schedule in
122 122
 # SchedulerAgent.  By default, the use of the "second" field is
123 123
 # restricted so that any value other than a single zero (which means
124
-# "on the minute") is disallowed.
124
+# "on the minute") is disallowed to prevent abuse of service.
125 125
 ENABLE_SECOND_PRECISION_SCHEDULE=false
126 126
 
127 127
 # Use Graphviz for generating diagrams instead of using Google Chart

+ 9 - 11
app/models/agents/scheduler_agent.rb

@@ -12,7 +12,7 @@ module Agents
12 12
 
13 13
     cattr_reader :second_precision_enabled
14 14
 
15
-    description <<-MD % { seconds: (<<-MD_SECONDS if second_precision_enabled) }
15
+    description <<-MD
16 16
       This agent periodically takes an action on target Agents according to a user-defined schedule.
17 17
 
18 18
       # Action types
@@ -48,7 +48,13 @@ module Agents
48 48
 
49 49
       * `0 22 * * 1-5 Etc/GMT+2`: every day of the week when it's 22:00 in GMT+2
50 50
 
51
-      %{seconds}
51
+      ## Seconds
52
+
53
+      You can optionally specify seconds before the minute field.
54
+
55
+      * `*/30 * * * * *`: every 30 seconds
56
+
57
+      #{"Only multiples of fifteen are allowed as values for the seconds field, i.e. `*/15`, `*/30`, `15,45` etc." unless second_precision_enabled}
52 58
 
53 59
       ## Last day of month
54 60
 
@@ -71,14 +77,6 @@ module Agents
71 77
       * `0 22 * * Sun#L1`: every last Sunday of the month, at 22:00
72 78
     MD
73 79
 
74
-      ## Seconds
75
-
76
-      You can optionally specify seconds before the minute field.
77
-
78
-      * `*/30 * * * * *`: every 30 seconds
79
-
80
-    MD_SECONDS
81
-
82 80
     def default_options
83 81
       super.update({
84 82
         'schedule' => '0 * * * *',
@@ -97,7 +95,7 @@ module Agents
97 95
       if (spec = options['schedule']).present?
98 96
         begin
99 97
           cron = Rufus::Scheduler::CronLine.new(spec)
100
-          if !second_precision_enabled && cron.seconds != [0]
98
+          unless second_precision_enabled || (cron.seconds - [0, 15, 45, 60]).empty?
101 99
             errors.add(:base, "second precision schedule is not allowed in this service")
102 100
           end
103 101
         rescue ArgumentError

+ 10 - 1
spec/models/agents/scheduler_agent_spec.rb

@@ -49,9 +49,18 @@ describe Agents::SchedulerAgent do
49 49
       @agent.should be_valid
50 50
 
51 51
       stub(@agent).second_precision_enabled { false }
52
-      @agent.options['schedule'] = '*/15 * * * * *'
52
+      @agent.options['schedule'] = '*/10 * * * * *'
53
+      @agent.should_not be_valid
54
+
55
+      @agent.options['schedule'] = '5/30 * * * * *'
53 56
       @agent.should_not be_valid
54 57
 
58
+      @agent.options['schedule'] = '*/15 * * * * *'
59
+      @agent.should be_valid
60
+
61
+      @agent.options['schedule'] = '15,45 * * * * *'
62
+      @agent.should be_valid
63
+
55 64
       @agent.options['schedule'] = '0 * * * * *'
56 65
       @agent.should be_valid
57 66
     end